﻿Imports System.IO

Partial Class Paginas_MantenerConvenios
    Inherits System.Web.UI.Page

#Region "Funciones y Procedimientos Internos"
    Private Sub CargarArchivo()
        Dim miFichero As HttpPostedFile= me.FileUpload1.postedfile
        Dim kb As Double = miFichero.ContentLength / 1024
        Dim nombreFichero As String = miFichero.FileName
        Dim mimeType As String = miFichero.ContentType.Trim
        Dim stream As FileStream = miFichero.InputStream
        miFichero.SaveAs(Server.MapPath("~/Paginas/img/Paso"))
    End Sub

    Private Sub CargarCombos()
        Call CargarCombosHora()
        Call UtilidadesPagina.CargarDropSINO(Me.ddlImpresion, "NO")

        Dim miCollect As New Collection
        Dim miNegTAdm As New dllNegocio.NegTiposAsistente(System.Configuration.ConfigurationManager.AppSettings.Get("SPECBDD"))
        Try
            miCollect = miNegTAdm.ListaTipoAsistenteVigente()
            Me.ddlTipoAsistencia.DataSource = miCollect
            Me.ddlTipoAsistencia.DataTextField = "Descripcion"
            Me.ddlTipoAsistencia.DataValueField = "Codigo"
            Me.ddlTipoAsistencia.DataBind()
        Catch ex As Exception
        Finally
            miCollect = Nothing
            miNegTAdm.Dispose()
            miNegTAdm = Nothing
        End Try

        Dim miCollectI As New Collection
        Dim miNegImgAdm As New dllNegocio.NegImagenes(System.Configuration.ConfigurationManager.AppSettings.Get("SPECBDD"))
        Try
            miCollectI = miNegImgAdm.ListaImagenesVigente()
            Me.ddlImagen.DataSource = miCollectI
            Me.ddlImagen.DataTextField = "Descripcion"
            Me.ddlImagen.DataValueField = "Codigo"
            Me.ddlImagen.DataBind()
        Catch ex As Exception
        Finally
            miCollectI = Nothing
            miNegImgAdm.Dispose()
            miNegImgAdm = Nothing
        End Try
    End Sub

    Private Sub CargarCombosHora()
        Dim HoraIni As DateTime = "2010/01/01 00:00:01"
        For x As Integer = 1 To (24 * 2)
            Me.ddlHorasIni.Items.Add(New ListItem(HoraIni.ToString("HH:mm"), CInt(HoraIni.ToString("HHmm"))))
            Me.ddlHorasFin.Items.Add(New ListItem(HoraIni.ToString("HH:mm"), CInt(HoraIni.ToString("HHmm"))))
            HoraIni = DateAdd(DateInterval.Minute, 30, HoraIni)
        Next
    End Sub

    Private Sub Inicializar()
        Me.btnAgregarLugar.Visible = False
        Me.chkVigente.Visible = False
        Call CargarCombos()
    End Sub

    Private Sub CargarConvenio()
        Dim miCollect As New Collection
        Dim miNegConv As New dllNegocio.NegConvenios(System.Configuration.ConfigurationManager.AppSettings.Get("SPECBDD"))
        Try
            miCollect = miNegConv.ListarConvenios(Me.hdnIdConvenio.Value.Trim, "TODOS", 0)
            If miCollect.Count > 0 Then
                Dim objConv As dllEntidades.clsConvenio
                objConv = CType(miCollect.Item(1), dllEntidades.clsConvenio)
                With objConv
                    Me.chkVigente.Checked = IIf(.Activo = "SI", True, False)
                    Me.hdnIdConvenio.Value = .Codigo
                    Me.ddlImagen.SelectedValue = .CodigoImagen
                    Me.ddlTipoAsistencia.SelectedValue = .CodigoTipoAsistente
                    Me.txtCondUso.Text = .Condiciones
                    Me.txtDescripcion.Text = .Descripcion
                    Me.txtFecIni.Text = .FechaHoraInicio.ToString("dd/MM/yyyy")
                    Me.ddlHorasIni.SelectedValue = .FechaHoraInicio.ToString("HHmm")
                    Me.txtFechaFin.Text = .FechaHoraFinal.ToString("dd/MM/yyyy")
                    Me.ddlHorasFin.SelectedValue = .FechaHoraFinal.ToString("HHmm")
                    Me.hdnImagen.Value = .ImagenMail
                    Me.ddlImpresion.SelectedValue = .ImprimeCupon
                    Me.txtNombre.Text = .Nombre
                End With
                Me.btnAgregarLugar.Visible = True
            End If
        Catch ex As Exception
        Finally
            miCollect = Nothing
            miNegConv.Dispose()
            miNegConv = Nothing
        End Try
    End Sub
#End Region

#Region "EVENTOS DE LA PAGINA"
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack() Then
            Call Inicializar()
            If (Request.QueryString("ConvenioID")) <> "" Then
                If Request.QueryString("ConvenioID") <> "0" Then
                    Me.hdnIdConvenio.Value = Request.QueryString("ConvenioID")
                    Call CargarConvenio()
                Else
                    Me.hdnIdConvenio.Value = "0"
                End If
            Else
                Me.hdnIdConvenio.Value = "0"
            End If
        End If
    End Sub
#End Region

    Protected Sub btnGrabar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGrabar.Click
        Dim objNeg As New dllNegocio.NegConvenios(System.Configuration.ConfigurationManager.AppSettings.Get("SPECBDD"))
        Dim objConvenio As New dllEntidades.clsConvenio
        Try
            With objConvenio
                .Activo = IIf(Me.chkVigente.Checked = True, "SI", "NO")
                .Codigo = Me.hdnIdConvenio.Value
                .CodigoImagen = Me.ddlImagen.SelectedItem.Value
                .CodigoTipoAsistente = Me.ddlTipoAsistencia.SelectedItem.Value
                .Condiciones = Me.txtCondUso.Text.Trim
                .Descripcion = Me.txtDescripcion.Text.Trim
                .FechaHoraFinal = Me.txtFechaFin.Text & " " & Me.ddlHorasFin.SelectedItem.Text.Trim & ":00"
                .FechaHoraInicio = Me.txtFecIni.Text & " " & Me.ddlHorasIni.SelectedItem.Text.Trim & ":00"
                .ImprimeCupon = Me.ddlImpresion.SelectedItem.Text
                .Nombre = Me.txtNombre.Text
            End With
            Dim ResulGrabar As Integer

            If Me.hdnIdConvenio.Value = 0 Then
                ResulGrabar = objNeg.GrabarConvenio(objConvenio)
                Dim nombreImagen As String = "Paginas/img/ImgLoad/Convenios/Convenio" & ResulGrabar.ToString.Trim
                Dim NombreImagenActual As String = Me.FileUpload1.FileName
                NombreImagenActual = NombreImagenActual.Split("\")(NombreImagenActual.Split("\").Length - 1)
                NombreImagenActual = NombreImagenActual.Split(".")(NombreImagenActual.Split(".").Length - 1)
                nombreImagen = nombreImagen & "." & NombreImagenActual
                Me.FileUpload1.SaveAs(Server.MapPath("~") & "/" & nombreImagen)
                NombreImagenActual = nombreImagen.Split("/")(nombreImagen.Split("/").Length - 1)
                objConvenio.ImagenMail = NombreImagenActual
                objConvenio.Codigo = ResulGrabar
                ResulGrabar = objNeg.UpdatearImagenConvenio(objConvenio)
            Else
                ResulGrabar = objNeg.UpdatearConvenio(objConvenio)
            End If

            If ResulGrabar > 0 Then
                Me.hdnIdConvenio.Value = ResulGrabar.ToString
                UtilidadesPagina.mensaje("Convenio guardado correctamente", Me.Page)
                Me.btnAgregarLugar.Visible = True
            End If
        Catch ex As Exception
        Finally
            objNeg.Dispose()
            objConvenio.Dispose()
            objNeg = Nothing
            objConvenio = Nothing
        End Try
    End Sub

    Protected Sub btnVolver_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnVolver.Click
        Response.Redirect("ListarConvenios.aspx?PostBack=SI")
    End Sub

    Protected Sub btnAgregarLugar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAgregarLugar.Click
        Response.Redirect("MantencionLugares.aspx?ConvenioID=" & Me.hdnIdConvenio.Value.Trim)
        Response.End()
    End Sub
End Class
